Getting started with R

From the very basics

Dr. Carina Nigg & dr. Judith Bouman

Introduction

Course content

Introduction of R and RStudio

  • Understanding R and RStudio
  • Using basic functions
  • Writing a first script
  • Understanding packages
  • Importing data
  • Using basic functions on imported data

Analyzing your own data

  • Organizing your data
  • Loading your data into R
  • Providing an overview of your data
  • Inspecting missing data
  • Checking plausibility of your data

Why learn R?

  • Reproducibility of your results
  • Free software
  • A lot of resources available
  • Potentially useful in further career

Disclaimers

  • First time we are teaching this course
  • We expect no prior knowledge
  • Ask questions
  • Tell us if we are too slow/fast

Installing R and RStudio

Did you all manage to install both R and RStudio?

Difference R and RStudio

Opening RStudio

Rstudio windows

Simple calculator

1 + 1 
[1] 2

Using a script

  • Track code
  • Make changes
  • Repeat code (reproducible science)

Now, you can download, save and open the “follow_along_script_monday_morning.R”

Code and comments

“#” can be use to add text and comments

# Use "#" to add text and explanations to your code 

Keyboard shortcuts

  • Run the current line or selection: Ctrl + Enter / Option + Enter
  • Run all code in the script: Ctrl + Alt + R / Command + shift + Enter
  • Interrupt running code: Esc
  • Comment/uncomment lines: Ctrl + Shift + C / Command + shift + C

Saving “objects”

a = 1 
b = 2
c = a + b

# can use = or "<-"

c <- a + b 

c
[1] 3

Why could this be helpful?

Example “objects”

data = c(1,13,2,1,43,53,1,2,34,54,2,4,6,23)

cutoff = 10 

data[data>cutoff]
[1] 13 43 53 34 54 23

Using “functions”

Using “functions”

sum(1, 2)
[1] 3
sum(a , b)
[1] 3
?sum

Getting help with functions

?sum

Vector

c(1,2,3)
[1] 1 2 3
c(a, b)
[1] 1 2

Vector

How to access an element in the vector

a_vector = c(1.23, 2.34, 6.21, 3.11, 3.412, 4.32, 5.922, 5.65)

a_vector[4]
[1] 3.11

Array/matrix

matrix(data = c(1,2,3,4,5,6,7,8), nrow = 4 )
     [,1] [,2]
[1,]    1    5
[2,]    2    6
[3,]    3    7
[4,]    4    8

Array/matrix

How to access an element from a matrix

a_matrix = matrix(data = c(1,2,3,4,5,6,7,8), nrow = 4 )

a_matrix[3,2] # first row, then column
[1] 7

Let`s try! – Exercise 1

Can you calculate the following for “a_vector”?

  • Mean
  • Standard deviation
  • Maximal value
  • Minimal value
  • Length of the vector
a_vector = c(1.23, 2.34, 6.21, 3.11, 3.412, 4.32, 5.922, 5.65)

Take 15 minutes to try

Let`s try! – Exercise 1 - Solution

a_vector = c(1.23, 2.34, 6.21, 3.11, 3.412, 4.32, 5.922, 5.65)

mean(a_vector)
[1] 4.02425
sd(a_vector)
[1] 1.811263
max(a_vector)
[1] 6.21
min(a_vector)
[1] 1.23
length(a_vector)
[1] 8

Let`s try! – Exercise 2

Can you figure out what you can do with the following functions?

  • seq()
  • rep()

Take 10 minutes

Let`s try! – Exercise 2 - Solution

seq(1,10)
 [1]  1  2  3  4  5  6  7  8  9 10
seq(1,10, by = 2)
[1] 1 3 5 7 9
rep(0,100)
  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Classes and types

Numeric and character

a_vector = c(1.23, 2.34, 6.21, 3.11, 3.412, 4.32, 5.922, 5.65)

class(a_vector)
[1] "numeric"
b_vector = c("something", "something else", "another thing", "completely differnt")

class(b_vector)
[1] "character"

Classes and types

Logical

c_vector = c(F, T, T,T, F, T, F, T)

class(c_vector)
[1] "logical"

Classes and types

factor

gender <- factor(c("Male", "Female", "Female", "Male"))

class(gender)
[1] "factor"

Combining different types of vectors

Lists

my_list <- list(name = "Alice", age = 25, scores = c(90, 85, 88))

print(my_list)
$name
[1] "Alice"

$age
[1] 25

$scores
[1] 90 85 88

Why do we care about classes and types?

  • Helpful for plotting –> we come back to this later
  • In functions the class/type of the several input variables are often pre-defined
#sum(c("Kees", "Klaas", "Jan"))

Data frame

# Create a data frame
df <- data.frame(x = 1:3, y = c("a", "b", "c"))

# Printing
print(df)
  x y
1 1 a
2 2 b
3 3 c
# use one of the inbuilt data frames
cars
   speed dist
1      4    2
2      4   10
3      7    4
4      7   22
5      8   16
6      9   10
7     10   18
8     10   26
9     10   34
10    11   17
11    11   28
12    12   14
13    12   20
14    12   24
15    12   28
16    13   26
17    13   34
18    13   34
19    13   46
20    14   26
21    14   36
22    14   60
23    14   80
24    15   20
25    15   26
26    15   54
27    16   32
28    16   40
29    17   32
30    17   40
31    17   50
32    18   42
33    18   56
34    18   76
35    18   84
36    19   36
37    19   46
38    19   68
39    20   32
40    20   48
41    20   52
42    20   56
43    20   64
44    22   66
45    23   54
46    24   70
47    24   92
48    24   93
49    24  120
50    25   85

Tibble

# Create a tibble
library(tibble)
tb <- tibble(x = 1:3, y = c("a", "b", "c"))

print(tb)
# A tibble: 3 × 2
      x y    
  <int> <chr>
1     1 a    
2     2 b    
3     3 c    

Introduction to simple plots

plot(cars$speed, cars$dist)

Introduction to simple plots

hist(cars$speed)

Let`s try! – Exercise 3

  • create a line plot for the sepal length against the sepal width of the iris data
  • color the line red
  • change x and y labelling
  • add a title
iris
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa
11           5.4         3.7          1.5         0.2     setosa
12           4.8         3.4          1.6         0.2     setosa
13           4.8         3.0          1.4         0.1     setosa
14           4.3         3.0          1.1         0.1     setosa
15           5.8         4.0          1.2         0.2     setosa
16           5.7         4.4          1.5         0.4     setosa
17           5.4         3.9          1.3         0.4     setosa
18           5.1         3.5          1.4         0.3     setosa
19           5.7         3.8          1.7         0.3     setosa
20           5.1         3.8          1.5         0.3     setosa
21           5.4         3.4          1.7         0.2     setosa
22           5.1         3.7          1.5         0.4     setosa
23           4.6         3.6          1.0         0.2     setosa
24           5.1         3.3          1.7         0.5     setosa
25           4.8         3.4          1.9         0.2     setosa
26           5.0         3.0          1.6         0.2     setosa
27           5.0         3.4          1.6         0.4     setosa
28           5.2         3.5          1.5         0.2     setosa
29           5.2         3.4          1.4         0.2     setosa
30           4.7         3.2          1.6         0.2     setosa
31           4.8         3.1          1.6         0.2     setosa
32           5.4         3.4          1.5         0.4     setosa
33           5.2         4.1          1.5         0.1     setosa
34           5.5         4.2          1.4         0.2     setosa
35           4.9         3.1          1.5         0.2     setosa
36           5.0         3.2          1.2         0.2     setosa
37           5.5         3.5          1.3         0.2     setosa
38           4.9         3.6          1.4         0.1     setosa
39           4.4         3.0          1.3         0.2     setosa
40           5.1         3.4          1.5         0.2     setosa
41           5.0         3.5          1.3         0.3     setosa
42           4.5         2.3          1.3         0.3     setosa
43           4.4         3.2          1.3         0.2     setosa
44           5.0         3.5          1.6         0.6     setosa
45           5.1         3.8          1.9         0.4     setosa
46           4.8         3.0          1.4         0.3     setosa
47           5.1         3.8          1.6         0.2     setosa
48           4.6         3.2          1.4         0.2     setosa
49           5.3         3.7          1.5         0.2     setosa
50           5.0         3.3          1.4         0.2     setosa
51           7.0         3.2          4.7         1.4 versicolor
52           6.4         3.2          4.5         1.5 versicolor
53           6.9         3.1          4.9         1.5 versicolor
54           5.5         2.3          4.0         1.3 versicolor
55           6.5         2.8          4.6         1.5 versicolor
56           5.7         2.8          4.5         1.3 versicolor
57           6.3         3.3          4.7         1.6 versicolor
58           4.9         2.4          3.3         1.0 versicolor
59           6.6         2.9          4.6         1.3 versicolor
60           5.2         2.7          3.9         1.4 versicolor
61           5.0         2.0          3.5         1.0 versicolor
62           5.9         3.0          4.2         1.5 versicolor
63           6.0         2.2          4.0         1.0 versicolor
64           6.1         2.9          4.7         1.4 versicolor
65           5.6         2.9          3.6         1.3 versicolor
66           6.7         3.1          4.4         1.4 versicolor
67           5.6         3.0          4.5         1.5 versicolor
68           5.8         2.7          4.1         1.0 versicolor
69           6.2         2.2          4.5         1.5 versicolor
70           5.6         2.5          3.9         1.1 versicolor
71           5.9         3.2          4.8         1.8 versicolor
72           6.1         2.8          4.0         1.3 versicolor
73           6.3         2.5          4.9         1.5 versicolor
74           6.1         2.8          4.7         1.2 versicolor
75           6.4         2.9          4.3         1.3 versicolor
76           6.6         3.0          4.4         1.4 versicolor
77           6.8         2.8          4.8         1.4 versicolor
78           6.7         3.0          5.0         1.7 versicolor
79           6.0         2.9          4.5         1.5 versicolor
80           5.7         2.6          3.5         1.0 versicolor
81           5.5         2.4          3.8         1.1 versicolor
82           5.5         2.4          3.7         1.0 versicolor
83           5.8         2.7          3.9         1.2 versicolor
84           6.0         2.7          5.1         1.6 versicolor
85           5.4         3.0          4.5         1.5 versicolor
86           6.0         3.4          4.5         1.6 versicolor
87           6.7         3.1          4.7         1.5 versicolor
88           6.3         2.3          4.4         1.3 versicolor
89           5.6         3.0          4.1         1.3 versicolor
90           5.5         2.5          4.0         1.3 versicolor
91           5.5         2.6          4.4         1.2 versicolor
92           6.1         3.0          4.6         1.4 versicolor
93           5.8         2.6          4.0         1.2 versicolor
94           5.0         2.3          3.3         1.0 versicolor
95           5.6         2.7          4.2         1.3 versicolor
96           5.7         3.0          4.2         1.2 versicolor
97           5.7         2.9          4.2         1.3 versicolor
98           6.2         2.9          4.3         1.3 versicolor
99           5.1         2.5          3.0         1.1 versicolor
100          5.7         2.8          4.1         1.3 versicolor
101          6.3         3.3          6.0         2.5  virginica
102          5.8         2.7          5.1         1.9  virginica
103          7.1         3.0          5.9         2.1  virginica
104          6.3         2.9          5.6         1.8  virginica
105          6.5         3.0          5.8         2.2  virginica
106          7.6         3.0          6.6         2.1  virginica
107          4.9         2.5          4.5         1.7  virginica
108          7.3         2.9          6.3         1.8  virginica
109          6.7         2.5          5.8         1.8  virginica
110          7.2         3.6          6.1         2.5  virginica
111          6.5         3.2          5.1         2.0  virginica
112          6.4         2.7          5.3         1.9  virginica
113          6.8         3.0          5.5         2.1  virginica
114          5.7         2.5          5.0         2.0  virginica
115          5.8         2.8          5.1         2.4  virginica
116          6.4         3.2          5.3         2.3  virginica
117          6.5         3.0          5.5         1.8  virginica
118          7.7         3.8          6.7         2.2  virginica
119          7.7         2.6          6.9         2.3  virginica
120          6.0         2.2          5.0         1.5  virginica
121          6.9         3.2          5.7         2.3  virginica
122          5.6         2.8          4.9         2.0  virginica
123          7.7         2.8          6.7         2.0  virginica
124          6.3         2.7          4.9         1.8  virginica
125          6.7         3.3          5.7         2.1  virginica
126          7.2         3.2          6.0         1.8  virginica
127          6.2         2.8          4.8         1.8  virginica
128          6.1         3.0          4.9         1.8  virginica
129          6.4         2.8          5.6         2.1  virginica
130          7.2         3.0          5.8         1.6  virginica
131          7.4         2.8          6.1         1.9  virginica
132          7.9         3.8          6.4         2.0  virginica
133          6.4         2.8          5.6         2.2  virginica
134          6.3         2.8          5.1         1.5  virginica
135          6.1         2.6          5.6         1.4  virginica
136          7.7         3.0          6.1         2.3  virginica
137          6.3         3.4          5.6         2.4  virginica
138          6.4         3.1          5.5         1.8  virginica
139          6.0         3.0          4.8         1.8  virginica
140          6.9         3.1          5.4         2.1  virginica
141          6.7         3.1          5.6         2.4  virginica
142          6.9         3.1          5.1         2.3  virginica
143          5.8         2.7          5.1         1.9  virginica
144          6.8         3.2          5.9         2.3  virginica
145          6.7         3.3          5.7         2.5  virginica
146          6.7         3.0          5.2         2.3  virginica
147          6.3         2.5          5.0         1.9  virginica
148          6.5         3.0          5.2         2.0  virginica
149          6.2         3.4          5.4         2.3  virginica
150          5.9         3.0          5.1         1.8  virginica
?plot 
Help on topic 'plot' was found in the following packages:

  Package               Library
  graphics              /Library/Frameworks/R.framework/Versions/4.2/Resources/library
  base                  /Library/Frameworks/R.framework/Resources/library


Using the first match ...

Take 15 minutes

Let`s try! – Exercise 3 – solution

  • create a point plot for the sepal length against the sepal width of the iris data
  • color the points red
  • change x and y labeling
  • add a title
  • change the type of points
plot(iris$Sepal.Length, iris$Sepal.Width, 
     type = "p", # plot points ("l" would give a line)
     pch = 4, # change the type of points
     col = "red", # color the points red
     xlab = "length", # change the x label
     ylab = "width", # change the y label 
     main = "Simple example plot") # add tittle

Take 15 minutes

Packages

Get access to specific set of functions

#install.packages("tidyverse")
library(tidyverse)

run “library()” every time you want to use any function from this package

Let’s try!

Get your data in R

Organize your data

project_dir

Organize your data

Choose your working directory

getwd() 
[1] "/Users/jb22m516/Documents/GitHub/getting_started_with_R/slides"
setwd("/Users/jb22m516/Documents/GitHub/getting_started_with_R/")

“Path” to file in R

A “path” tells R where it can find your data

Functions for loading data

#install.packages("utils")
library(utils)

#data = read.csv2()

Types of data in R

tibble / dataframe / matrix / array